package net.csdn.business.notify.handler.config;



import net.csdn.business.notify.common.enums.ProcessTypeEnum;
import net.csdn.business.notify.common.pipeline.ProcessController;
import net.csdn.business.notify.common.pipeline.ProcessTemplate;
import net.csdn.business.notify.handler.action.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/**
 * @ClassName TaskPipelineConfig
 * @Description handler层的pipeline配置类
 * @Author zhangyl
 * @Date 2023/11/30 16:58
 */
@Configuration
public class HandlerPipelineConfig {

    @Autowired
    private RemindShieldAction remindShieldAction;
    @Autowired
    private SaveRemindAction saveRemindAction;
    @Autowired
    private DeduplicationAction deduplicationAction;
    @Autowired
    private SendMessageAction sendMessageAction;


    @Autowired
    private PrivateShieldAction privateShieldAction;
    @Autowired
    private SavePrivateAction savePrivateAction;

    @Autowired
    private SaveDirectAction saveDirectAction;



    /**
     * 通知消息从MQ消费的流程
     * 1.根据用户配置，是否屏蔽消息
     * 2.根据接收者信息，查询用户接收账号信息并将发送记录到消息记录表
     * 3.通用去重功能
     * 4.选择通道发送消息
     *
     * @return
     */
    @Bean("taskTemplate")
    public ProcessTemplate taskTemplate() {
        ProcessTemplate processTemplate = new ProcessTemplate();
        processTemplate.setProcessList(Arrays.asList(remindShieldAction,saveRemindAction, deduplicationAction, sendMessageAction));
        return processTemplate;
    }


    /**
     * 用户私信从MQ消费的流程
     * 1.根据用户关注关系，是否屏蔽消息
     * 2.保存私信
     *
     * @return
     */
    @Bean("taskPrivateTemplate")
    public ProcessTemplate taskPrivateTemplate() {
        ProcessTemplate processTemplate = new ProcessTemplate();
        processTemplate.setProcessList(Arrays.asList(privateShieldAction,savePrivateAction));
        return processTemplate;
    }

    /**
     * 直达消息：营销类、验证码从MQ消费的流程
     * 1.保存通知记录
     * 2.下发给通知渠道发送
     *
     * @return
     */
    @Bean("taskDirectTemplate")
    public ProcessTemplate taskDirectTemplate() {
        ProcessTemplate processTemplate = new ProcessTemplate();
        processTemplate.setProcessList(Arrays.asList(saveDirectAction,sendMessageAction));
        return processTemplate;
    }

    /**
     * pipeline流程控制器
     * 后续扩展则加BusinessCode和ProcessTemplate
     *
     * @return
     */
    @Bean("handlerProcessController")
    public ProcessController processController() {
        ProcessController processController = new ProcessController();
        Map<String, ProcessTemplate> templateConfig = new HashMap<>(10);
        templateConfig.put(ProcessTypeEnum.HANDLER_REMIND_NOTIFY.getCode(), taskTemplate());
        templateConfig.put(ProcessTypeEnum.HANDLER_PRIVATE_NOTIFY.getCode(), taskPrivateTemplate());
        templateConfig.put(ProcessTypeEnum.HANDLER_DIRECT_NOTIFY.getCode(), taskDirectTemplate());
        processController.setTemplateConfig(templateConfig);
        return processController;
    }
}
